package com.liusha.quicksand.web;

import com.liusha.quicksand.po.Comment;
import com.liusha.quicksand.po.User;
import com.liusha.quicksand.service.BlogService;
import com.liusha.quicksand.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/23
 * @Time: 17:09
 * @Description: No Description
 */

@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;

    @Value("${comment.avatar}")
    private String avatar;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {

        model.addAttribute("comments", commentService.listCommentByBlogId(blogId));

        return "blog :: commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session) {

        Long blogId = comment.getBlog().getBlogId();
        comment.setBlog(blogService.getBlog(blogId));
        User user = (User) session.getAttribute("user");
        if (user != null) {
            comment.setAvatar(user.getAvatar());
            comment.setBlogger(true);
            comment.setNickname(user.getNickname());
        } else {
            comment.setAvatar(avatar);
        }
        comment.setAvatar(avatar);
        commentService.saveComment(comment);

        return "redirect:/comments/" + blogId;
    }

    @GetMapping("/admin/comments")
    public String listComment(@PageableDefault(size = 30, direction = Sort.Direction.ASC) String kws, Model model, Pageable pageable) {

        model.addAttribute("kws", kws);
        if (kws != null) kws = "%" + kws + "%";
        if (kws == null) kws = "%" + "" + "%";

        Page<Comment> comments = commentService.listComment(pageable);
        model.addAttribute("pages", comments);

        System.out.println(comments);

        return "admin/comments";
    }

//    @RequestMapping("/admin/comments")
//    public String search(@PageableDefault(size = 30, direction = Sort.Direction.ASC) String kws, Model model, Pageable pageable) {
//
//        model.addAttribute("kws", kws);
//        if (kws != null) kws = "%" + kws + "%";
//        if (kws == null) kws = "%" + "" + "%";
//
//
//        Page<Comment> comments = commentService.listComm(kws, pageable);
//        model.addAttribute("pages", comments);
//
//        System.out.println(comments);
//        System.out.println(kws);
//
//        return "admin/comments";
//    }

    @GetMapping("/deleteComment/{commentId}")
    public String deleteComment(@PathVariable("commentId") Long commentId) {

        commentService.deleteById(commentId);

        return "redirect:/admin/comments";
    }
}
